<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.text.*"%>
<%@ include file="Connections/conDB.jsp" %>
<% int validaSession = 2; %>
<%@ include file="Connections/session.jsp" %>

<%

String parId_movimiento = request.getParameter ("id_movimiento");
String rsId_almacen = "";
String rsId_tipograno = "";
String rsId_lote = "";
String rsCantidad = "";

Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
String query ="";
PreparedStatement StatementRs =null;
ResultSet Rsquery = null;

if (parId_movimiento!=null && !parId_movimiento.equals("")) {
	// extrae la informacion de almacen, lote, tipo_grano y cantidad de vista_movimiento
	query = "SELECT "+
			"almacen, tipo_grano, lote, cantidad "+
			"FROM vista_movimiento "+
			"WHERE id_movimiento = "+parId_movimiento;
	//out.print(query);
	StatementRs = ConnRs.prepareStatement(query);
	Rsquery = StatementRs.executeQuery();
	if (Rsquery.next()) {
		rsId_almacen = Rsquery.getString("almacen");
		rsId_tipograno = Rsquery.getString("tipo_grano");
		rsId_lote = Rsquery.getString("lote");
		rsCantidad = Rsquery.getString("cantidad");
		double rsCantidadS = rsCantidad!=null && !rsCantidad.equals("")?Double.parseDouble(rsCantidad.trim()):0.0;
		rsCantidadS = (rsCantidadS)*-1.0;

		
		// actualiza la existencia
		query = "UPDATE existencia SET "+
				"existencia = existencia + "+rsCantidadS+" "+
				"WHERE almacen ="+rsId_almacen+" AND tipo_grano ="+rsId_tipograno+" AND lote ="+rsId_lote;
		//out.print(query);
		StatementRs = ConnRs.prepareStatement(query);
		StatementRs.executeUpdate();

		// elimina el movimiento
		query= "DELETE movimiento WHERE id_movimiento = "+parId_movimiento;
		//out.print(query);
		StatementRs=ConnRs.prepareStatement(query);
		StatementRs.executeUpdate();

	}
	Rsquery.close();


} // fin if (parId_movimiento!=null && !parId_movimiento.equals(""))

	out.print("<script>");
	//out.print("window.opener.location.reload();");
	out.print("window.opener.form1.submit();");
	out.print("window.close();");
	out.print("</script>");

ConnRs.close();

%>


<html>
<head>
<title>CIMA-Eliminar Movimiento</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="miestilo.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<center>
<br>
  <table width="250" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align="center">El Movimiento se a Eliminado.</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="center"> 
        <input type="button" name="Button" value="Aceptar" class="boton" onClick="window.opener.location.reload();window.close()">
      </td>
    </tr>
  </table>
</center>
</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
